<?php
class download
{
 private $db ;
 private $path = "../zip/download/";
 private $filename = "archive_";
 public $result ;

 
        function __construct($input)
        {
            // global $DATABASE;
            // $rows = $DATABASE->select('SELECT * FROM category'); 
            /*
         $this -> db = new db;
         $this -> online_users = false;
             * */
           // $createZip = new createZip;
            $this->filename .=$this->createName().".zip";
           $rows = $this->getAllRecFromDB($input);
           $this->checkFile();
           $this->rowsToFile($rows);
         
        }
 
        private function rowsToFile($rows)
        {   
          $createZip = new createZip; 
          
              foreach ($rows as $key=>$value){
                   
                        $fileContents = $value[0]['post_content'];
                        $fileName = $value[0]['post_title']."_id_".$value[0]['ID'].".txt";
                        $createZip->addFile($fileContents, $fileName);
                    
            }
          
       
                //$createZip->addDirectory("zip/download");
                $fd = fopen($this->path.$this->filename, "wb");
                $out = fwrite($fd, $createZip->getZippedfile());
                fclose($fd);
          
      
                $this->result = "load.php?filename=$this->filename";
               // $this->otdatFile($this->filename);
              //  $this->checkFile();
      
           
        }
 
        private function getAllRecFromDB($id)
        {
            global $DATABASE;
            foreach ($id as $key=>$value){
                $rows[] = $DATABASE->select('SELECT post_content, post_title, ID FROM posts WHERE posts.ID=?', $value);
            
            }
                return $rows;
        }
        
        private function checkFile()
        {
           if (file_exists($this->path.$this->filename)) unlink($this->path.$this->filename); 
        }
        
        private function createName() {
		$chars = "abcdefghijkmnopqrstuvwxyz023456789";
		srand((double)microtime()*1000000);
		$i = 0;
		$pass = '' ;
		while ($i <= 7) {
			$num = rand() % 33;
			$tmp = substr($chars, $num, 1);
			$pass = $pass . $tmp;
			$i++;
		}
		return $pass;
	}
       


}

?>
